package com.hoshiicloud.market.controller.market;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.market.entity.market.ShareInfo;
import com.hoshiicloud.market.service.market.ShareInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author yangcheng
 * @version 1.0
 * @title: ShareInfoController
 * @description: 分享信息Controller
 * @date 2019/8/2 14:13
 * @mail maiboyc@163.com
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shareInfo")
@Api(value = "分享信息", tags = {"分享信息管理"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class ShareInfoController {

    // 分享信息Service
    @Autowired
    private ShareInfoService shareInfoService;

    /**
     * 分页查询分享信息
     * @param param
     * @return Response
     */
    @ApiOperation(value = "分页查询分享信息")
    @PostMapping("/page")
    public Response page(@RequestBody ShareInfo param) {
        try {
            log.info("分页查询分享信息");
            QueryWrapper<ShareInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(param.getCompanyId() != null,ShareInfo::getCompanyId,param.getCompanyId())
                .eq(param.getPlatformId() != null, ShareInfo::getPlatformId, param.getPlatformId())
                .eq(param.getUserId() != null, ShareInfo::getUserId, param.getUserId())
                .eq(param.getType() != null, ShareInfo::getType, param.getType())
                .eq(param.getBusinessId() != null, ShareInfo::getBusinessId, param.getBusinessId())
                .eq(param.getPage() != null, ShareInfo::getPage, param.getPage())
                .eq(param.getUrl() != null, ShareInfo::getUrl, param.getUrl())
                .eq(ShareInfo::getDeleted,0);
            IPage<ShareInfo> page = new Page<>(param.getPageNumber(), param.getPageSize());
            return Response.data(shareInfoService.page(page, queryWrapper,param.toString()));
        } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("分页查询分享信息异常", e);
            return Response.failed("分页查询分享信息异常"+e.getMessage());
        }
    }

    /**
     * 查询分享信息
     * @param param 查询参数
     * @return Response<List<ShareInfo>>
     */
    @ApiOperation(value = "查询分享信息")
    @PostMapping("/list")
    public Response<List<ShareInfo>> list(@RequestBody ShareInfo param) {
        try {
            log.info("查询分享信息");
            QueryWrapper<ShareInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(param.getCompanyId() != null,"company_id",param.getCompanyId());
            queryWrapper.eq(param.getPlatformId() != null, "platform_id", param.getPlatformId());
            if (null != param.getUserId()) {
                queryWrapper.eq("user_id",param.getUserId());
            } else {
                queryWrapper.isNull("user_id");
            }
            queryWrapper.eq(param.getType() != null, "type", param.getType());
            queryWrapper.eq(param.getBusinessId() != null, "business_id", param.getBusinessId());
            queryWrapper.eq(param.getPage() != null, "page", param.getPage());
            queryWrapper.eq(param.getUrl() != null, "url", param.getUrl());
            queryWrapper.eq("deleted",0);
            return Response.data(shareInfoService.list(queryWrapper,param.toString()));
        } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
        } catch (Exception e) {
            log.error("查询分享信息异常", e);
            return Response.failed("查询分享信息异常"+e.getMessage());
        }
    }

    /**
     * 根据id查询分享信息
     * @param id 主键ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询分享信息")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(shareInfoService.getById(id));
    }

    /**
     * 新增分享信息
     * @param param 新增参数
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增分享信息")
    public Response<Long> save(@RequestBody @Valid ShareInfo param) {
        try {
            return Response.data(shareInfoService.insert(param));
        } catch (ServiceException e) {
            log.error("新增分享信息异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增分享信息异常", e);
            return Response.failed();
        }
    }

    /**
     * 修改分享信息
     * @param param 修改参数
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改分享信息")
    public Response update(@RequestBody @Valid ShareInfo param) {
        try {
            return Response.data(shareInfoService.updateById(param));
        }catch (ServiceException e) {
            log.error("修改分享信息异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改分享信息异常", e);
            return Response.failed();
        }
    }

    /**
     * 删除分享信息
     * @param id 主键ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除分享信息")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            ShareInfo data = shareInfoService.getById(id);
            if(data==null){
                log.error("删除分享信息异常，无法找到分享信息记录");
                return Response.failed("删除分享信息异常，无法找到分享信息记录");
            } else {
                return Response.data(shareInfoService.deleteById(data));
            }
        }catch (ServiceException e) {
            log.error("删除分享信息异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除分享信息异常", e);
            return Response.failed("删除分享信息异常："+e.getMessage());
        }
    }
    
}
